package com.ecreditpal.audio.model;

import com.ecreditpal.audio.model.AudioModel;
import lombok.Getter;
import lombok.Setter;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.LinkedBlockingQueue;

/**
 * @author lifeng
 * @version 1.0 on 2018/7/12.
 */
@Getter
@Setter
public class AudioSession {
    private LinkedBlockingQueue<AudioModel.Audio> queue = new LinkedBlockingQueue<>();

    private volatile boolean isStarted = false;

    private String caseId;
    private String callId;

    private byte[] localLeft;
    private Double localTotalOffset = 0.0;
    private boolean isLocalLongChunk = false;

    private byte[] remoteLeft;
    private Double remoteTotalOffset = 0.0;
    private boolean isRemoteLongChunk = false;

    private List<SpeechRecord> local = new ArrayList<>();
    private List<SpeechRecord> remote = new ArrayList<>();

    private int byteRate = 16000 * 16 / 8;


    public AudioSession() {
    }

    public AudioSession(int byteRate) {
        this.byteRate = byteRate;
    }



    public void clearSession() {
        isStarted = false;

        localLeft = null;
        localTotalOffset = 0.0;
        isLocalLongChunk = false;

        remoteLeft = null;
        remoteTotalOffset = 0.0;
        isRemoteLongChunk = false;

        caseId = null;
        callId = null;

    }

    public byte[] getLeftByType(AudioModel.AudioType audioType) {
        if (audioType == AudioModel.AudioType.LOCAL) {
            return localLeft;
        } else {
            return remoteLeft;
        }
    }


    public double getSessionOffset(AudioModel.AudioType audioType) {
        if (audioType == AudioModel.AudioType.LOCAL) {
            return localTotalOffset;
        } else {
            return remoteTotalOffset;
        }
    }

    public boolean isLongChunk(AudioModel.AudioType audioType) {
       return audioType == AudioModel.AudioType.LOCAL? isLocalLongChunk:isRemoteLongChunk;
    }

    public void setLongChunk(AudioModel.AudioType audioType,boolean isLongChunk) {
        if (audioType == AudioModel.AudioType.LOCAL) {
            setLocalLongChunk(isLongChunk);
        } else {
            setRemoteLongChunk(isLongChunk);
        }
    }


    public void setSessionOffset(AudioModel.AudioType audioType,double offset) {
        if (audioType == AudioModel.AudioType.LOCAL) {
            setLocalTotalOffset(offset);
        } else {
            setRemoteTotalOffset(offset);
        }
    }


    public void setRecord(AudioModel.AudioType audioType,int start,int end,String record) {
        SpeechRecord r = new SpeechRecord();
        r.setBegin(start);
        r.setEnd(end);
        r.setRecord(record);
        if (audioType == AudioModel.AudioType.LOCAL) {
           local.add(r);
        }else {
           remote.add(r);
        }
    }







}
